/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-08-25
 * Time: 18:35
 */
public class WorkDemo {
    //Day5
    //1.
    public int GetNumberOfK(int [] array , int k) {
        int left = 0;
        int right = array.length - 1;
        int count = 0;
        while (left < right){
            while (array[left] != k && left < right){
                left++;
            }
            while (array[right] != k && left < right){
                right--;
            }
            if (left < right){
                count += 2;
                left++;
                right--;
            }
        }
        if (left == right && array[left] == k){
            count++;
        }
        return count;
    }
    //2.
    public int convertInteger(int A, int B) {
        int ret = A ^ B;
        int count = 0;
        while (ret != 0){
            ret = ret & (ret - 1);
            count++;
        }
        return count;
    }

    //Day6
    //1.A 2.A 3.C 4.D 5.A

    //6.至少是其他数字两倍的最大数字
    public int dominantIndex(int[] nums) {
        int max = nums[0];
        int min = nums[0];
        int ret = 0;
        for (int i = 1; i < nums.length; i++) {
            if (max < nums[i]){
                max = nums[i];
                ret = i;
            }
            if (min > nums[i]){
                min = nums[i];
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (max != nums[i] && min < nums[i]){
                min = nums[i];
            }
        }
        if (max >= 2 * min){
            return ret;
        } else {
            return -1;
        }
    }

    //7.两个数组的交集
    public int[] intersection(int[] nums1, int[] nums2) {
        int i = 0;
        int j = 0;
        int k = 0;
        int[] ret = new int[1000];
        while (i < nums2.length){
            for (j = 0; j < nums2.length; j++) {
                if (nums1[i] == nums2[j] && nums1[i] != ret[0]){
                    ret[k] = nums1[i];
                    k++;
                    nums1[i] = ret[0];
                }
            }
            i++;
        }
        return ret;
    }
}
